import sys
import os
import time
import urllib

#host = sys.argv[1]
#sh_ypgfile = open('./createYPG.sh','w')
#sh_ypgfile.write("#!/bin/bash\n")

def executeSH(cmd):
	#cmd = "sh %s" %sh_ypgfile.name
	#print cmd
	os.system(cmd)
	print "done %s" %cmd

def getParents(crosskey):
	print "Get parents for key"

def getSiblings(key):
	remainder = key%4
	if (remainder == 0):
		return "'YPG%20" + str(key + 1) + "_YPG%20" + str(key + 2) + "_YPG%20" + str(key + 3) + "'"
	elif (remainder == 1):
		return "'YPG%20" + str(key - 1) + "_YPG%20" + str(key + 1) + "_YPG%20" + str(key + 2) + "'"
	elif (remainder == 2):
		return "'YPG%20" + str(key - 1) + "_YPG%20" + str(key - 2) + "_YPG%20" + str(key + 1) + "'"
	else:
		return "'YPG%20" + str(key - 1) + "_YPG%20" + str(key - 2) + "_YPG%20" + str(key - 3) + "'"

def translateRow(rowInt):
	if (rowInt == 0):
		return 'A'
	elif (rowInt == 1):
		return 'B'
	elif (rowInt == 2):
		return 'C'
	elif (rowInt == 3):
		return 'D'
	elif (rowInt == 4):
		return 'E'
	elif (rowInt == 5):
		return 'F'
	elif (rowInt == 6):
		return 'G'
	elif (rowInt == 7):
		return 'H'
	else:
		return 'Exception'

def translatePosition(key):
	#total/12 gets row, translate 0 = A, 1 = B, 2, C, ... 7 = H since only 96 wells
	#key mod 12 will get the rows
	return translateRow((key-1)/12) + str((key-1)%12 + 1)

def translateBoxNumber(key):
	return '-' + str(key/96 + 1)
		
def encodeSpace(str):
	return str.replace(' ', '%20')

#encode url
#python POST.py "http://ravioli:8080/addama-rest/dudley-sample/path/datasource/YPG/YPG%209999/create?JSON={genotype:'ggg',crossingRef:'YCR%208'}"
def generateCreatePosts(crosskey, start, end, parentAKey, parentAlphaKey):
	global host, sh_ypgfile, mode
	#print "crosskey %s" %crosskey
	sh_ypgfile.write("#!/bin/bash\n")
	#need to compute box_number, begin with 1, and every 96 strains, increment
	for ypgkey in range(start, end+1):
		postUrl = 'python POST.py "http://' + host + ':8080/addama-rest/dudley-sample/path/datasource/YPG/YPG%20' + str(ypgkey) + '/' + mode + '?JSON='
		params = "{strainName:'YPG%20" + str(ypgkey) +"',matingType:'Undetermined',parentA:'YPG%20" + parentAKey + "',parentAlpha:'YPG%20" + parentAlphaKey + "',siblings:" + getSiblings(ypgkey) + ",comments:'" + encodeSpace('ZhiHao fluffy strains') + "',dataFrozen:'04/01/2011',crossingRef:'YCR%20" + crosskey + "',boxNumber:'YCR%20" + crosskey + translateBoxNumber(ypgkey) + "',position:'" + translatePosition(ypgkey) + "'}"
		sh_ypgfile.write(postUrl + params + '"\n')
	
	sh_ypgfile.close()
	executeSH('sh ' + sh_ypgfile.name)

if __name__ == "__main__":
	global host, sh_ypgfile, ypgCount, mode
	if (len(sys.argv) < 7):
		print "authorization to host\npy2.6 authorization.py http://ravioli:8443\npython(2.5+) required \npy2.6 YPGForYCR.py host ycrKey ypgKeyStart ypgKeyEnd parentAKey parentAlphaKey"
		sys.exit(1)
	else:
		mode = 'create'
		host = sys.argv[1]
		ypgCount = int(sys.argv[4])
		sh_ypgfile = open('./createYPG%s.sh' %('ycr'+sys.argv[2]),'w')
		if (len(sys.argv) > 7):
			mode = sys.argv[7]
		generateCreatePosts(sys.argv[2], int(sys.argv[3]),int(sys.argv[4]), sys.argv[5], sys.argv[6])
